﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;

namespace WpfApplication1
{
    //这个程序集主要处理逻辑问题
    #region 记录辅助类
    //记录第一周起始日期
    public class RecordEveryday
    {
        static public DateTime StartDay=new DateTime(2010,9,5);
    }

    //记录每一条课程记录
    public class CourseItem
    {
        public String Address;
        public int From;
        public int To;
        public int Time;
        public int Week;
        public CourseItem(int from, int to, int time, int week, String address)
        {
            From = from;
            To = to;
            Time = time;
            Week = week;
            Address = address;
        }
        public override string ToString()
        {
            return From.ToString() + " " + To.ToString() + " " + Address.ToString() + " " + Time.ToString() + " " + Week.ToString();
        }
    }

    //记录每一个文件路径
    public class LinkItem
    {
        public String kind="";
        public String path="";
        public LinkItem(String k, String p)
        {
            kind = k;
            path = p;
        }
        static public LinkItem NULL = new LinkItem("", "");
        public override string ToString()
        {
            return path;
        }
    }

    //记录每时段详细信息
    public class RecordItem
    {
        public String CourseName="";
        public String CourseAddress="";
        public List<LinkItem> LinkList = new List<LinkItem>();
        public String ThingsContent="";
        public RecordItem()
        {
        }
        public RecordItem(String cn, String ca, String tc)
        {
            CourseAddress = ca;
            CourseName = cn;
            ThingsContent = tc;
        }
        static public RecordItem NULL = new RecordItem("", "", "");
    }
    #endregion

#region 文件输入输出类
    //主要负责操作xml文件的写入和读取
    public class CourseRecord
    {
        public  String name="";
        public String teacher = "";
        public String credit = "";
        public CourseRecord(String name, String teacher, String credit)
        {
            this.name = name;
            this.teacher = teacher;
            this.credit = credit;
            if (name != "")
            {
                XmlDocument xd = new XmlDocument();
                XmlElement xmlelem, xmlelem1;
                XmlNode xmlnode;
                if (File.Exists("course.xml"))
                {
                    try
                    {
                        xd.Load("course.xml");
                    }
                    catch
                    {
                        //加入xml声明
                        xmlnode = xd.CreateNode(XmlNodeType.XmlDeclaration, "", "");
                        xd.AppendChild(xmlnode);
                        //加入一个根元素
                        xmlelem = xd.CreateElement("", "ROOT", "");
                        xd.AppendChild(xmlelem);

                    }
                }
                else
                {
                    //加入xml声明
                    xmlnode = xd.CreateNode(XmlNodeType.XmlDeclaration, "", "");
                    xd.AppendChild(xmlnode);
                    //加入一个根元素
                    xmlelem = xd.CreateElement("", "ROOT", "");
                    xd.AppendChild(xmlelem);
                }
                foreach (XmlNode xn in xd.ChildNodes.Item(1).ChildNodes)
                {
                    if (xn.ChildNodes.Item(0).ChildNodes.Item(0).Value == name)
                    {
                        return;
                    }
                }
                xmlelem = xd.CreateElement("Course");
                xmlelem1 = xd.CreateElement("Name");
                xmlelem1.AppendChild(xd.CreateTextNode(name));
                xmlelem.AppendChild(xmlelem1);
                xmlelem1 = xd.CreateElement("Teacher");
                xmlelem1.AppendChild(xd.CreateTextNode(teacher));
                xmlelem.AppendChild(xmlelem1);
                xmlelem1 = xd.CreateElement("Credit");
                xmlelem1.AppendChild(xd.CreateTextNode(credit));
                xmlelem.AppendChild(xmlelem1);
                xd.ChildNodes.Item(1).AppendChild(xmlelem);
                xd.Save("course.xml");
            }
        }
        public void AddRecord(int From,int To,int weeknum,int timenum,String address)
        {
            XmlDocument xd = new XmlDocument();
            XmlElement xmlelem;
            XmlNode xmlnode;
            if (File.Exists("everyday.xml"))
            {
                try
                {
                    xd.Load("everyday.xml");
                }
                catch
                {
                    //加入xml声明
                    xmlnode = xd.CreateNode(XmlNodeType.XmlDeclaration, "", "");
                    xd.AppendChild(xmlnode);
                    //加入一个根元素
                    xmlelem = xd.CreateElement("", "ROOT", "");
                    xd.AppendChild(xmlelem);
                }
            }
            else
            {
                //加入xml声明
                xmlnode = xd.CreateNode(XmlNodeType.XmlDeclaration, "", "");
                xd.AppendChild(xmlnode);
                //加入一个根元素
                xmlelem = xd.CreateElement("", "ROOT", "");
                xd.AppendChild(xmlelem);
            }
            for (int i = From; i <= To; i++)
            {
                DateTime needDay=RecordEveryday.StartDay.AddDays((i-1) * 7 + weeknum);
                xmlnode=CourseRecord.FindAndInsertTime(xd, xd.ChildNodes.Item(1), needDay.Year, needDay.Month, needDay.Day, timenum);
                xmlelem = xd.CreateElement("Course");
                xmlelem.SetAttribute("Name", name);
                xmlelem.SetAttribute("Address", address);
                xmlnode.AppendChild(xmlelem);
            }
            xd.Save("everyday.xml");
        }
        static public void AddThings(int year, int month, int day, int time,String Text)
        {
            XmlDocument xd = new XmlDocument();
            XmlElement xmlelem;
            XmlNode xmlnode;
            if (File.Exists("everyday.xml"))
            {
                try
                {
                    xd.Load("everyday.xml");
                }
                catch
                {
                    //加入xml声明
                    xmlnode = xd.CreateNode(XmlNodeType.XmlDeclaration, "", "");
                    xd.AppendChild(xmlnode);
                    //加入一个根元素
                    xmlelem = xd.CreateElement("", "ROOT", "");
                    xd.AppendChild(xmlelem);
                }
            }
            else
            {
                //加入xml声明
                xmlnode = xd.CreateNode(XmlNodeType.XmlDeclaration, "", "");
                xd.AppendChild(xmlnode);
                //加入一个根元素
                xmlelem = xd.CreateElement("", "ROOT", "");
                xd.AppendChild(xmlelem);
            }
            xmlnode = FindAndInsertTime(xd, xd.ChildNodes.Item(1), year,month,day,time);
            foreach (XmlNode xn in xmlnode.ChildNodes)
            {
                if (xn.Name == "Things")
                {
                    xn.FirstChild.Value = Text;
                    xd.Save("everyday.xml");
                    return;
                }
            }
            xmlelem = xd.CreateElement("Things");
            xmlelem.AppendChild(xd.CreateTextNode(Text));
            xmlnode.AppendChild(xmlelem);
        
            xd.Save("everyday.xml");
        }
        static public void AddLink(int year,int month,int day,int time,String kind,String path)
        {
            XmlDocument xd = new XmlDocument();
            XmlElement xmlelem;
            XmlNode xmlnode;
            if (File.Exists("everyday.xml"))
            {
                try
                {
                    xd.Load("everyday.xml");
                }
                catch
                {
                    //加入xml声明
                    xmlnode = xd.CreateNode(XmlNodeType.XmlDeclaration, "", "");
                    xd.AppendChild(xmlnode);
                    //加入一个根元素
                    xmlelem = xd.CreateElement("", "ROOT", "");
                    xd.AppendChild(xmlelem);
                }
            }
            else
            {
                //加入xml声明
                xmlnode = xd.CreateNode(XmlNodeType.XmlDeclaration, "", "");
                xd.AppendChild(xmlnode);
                //加入一个根元素
                xmlelem = xd.CreateElement("", "ROOT", "");
                xd.AppendChild(xmlelem);
            }
            xmlnode = FindAndInsertTime(xd, xd.ChildNodes.Item(1), year, month, day, time);
            xmlelem = xd.CreateElement("Link");
            xmlelem.SetAttribute("Kind", kind);
            xmlelem.SetAttribute("Path", path);
            xmlnode.AppendChild(xmlelem);

            xd.Save("everyday.xml");
        }
        static public RecordItem getDetails(int year, int month, int day, int time)
        {
            XmlDocument xmldoc = new XmlDocument();
            try
            {
                xmldoc.Load("everyday.xml");
                XmlNode xmlnode = FindTime(xmldoc.ChildNodes.Item(1), year, month, day, time);
                if (xmlnode == null)
                {
                    return RecordItem.NULL;
                }
                else
                {
                    RecordItem record = new RecordItem();
                    foreach (XmlNode xn in xmlnode.ChildNodes)
                    {
                        switch (xn.Name)
                        {
                            case "Course":
                                record.CourseName = xn.Attributes[0].Value;
                                record.CourseAddress = xn.Attributes[1].Value;
                                break;
                            case "Things":
                                record.ThingsContent = xn.ChildNodes.Item(0).Value;
                                break;
                            case "Link":
                                record.LinkList.Add(new LinkItem(xn.Attributes["Kind"].Value,xn.Attributes["Path"].Value));
                                break;
                        }
                    }
                    return record;
                }
            }
            catch
            {
                return RecordItem.NULL;
            }
        }
        static public CourseRecord getCourseDetail(String name)
        {
            if (File.Exists("course.xml"))
            {
                XmlDocument xd = new XmlDocument();
                xd.Load("course.xml");
                foreach (XmlNode xn in xd.ChildNodes.Item(1).ChildNodes)
                {
                    if (xn.ChildNodes.Item(0).ChildNodes.Item(0).Value == name)
                    {
                        return new CourseRecord(name, xn.ChildNodes.Item(1).ChildNodes.Item(0).Value, xn.ChildNodes.Item(2).ChildNodes.Item(0).Value);
                    }
                }
            }
            return new CourseRecord("", "", "");
        }
        static private XmlNode FindTime(XmlNode root, int year, int month, int day, int time)
        {
            foreach (XmlNode xn0 in root.ChildNodes)
            {
                if (xn0.Attributes["value"].Value == year.ToString())
                {
                    foreach (XmlNode xn1 in xn0.ChildNodes)
                    {
                        if (xn1.Attributes["value"].Value == month.ToString())
                        {
                            foreach (XmlNode xn2 in xn1.ChildNodes)
                            {
                                if (xn2.Attributes["value"].Value == day.ToString())
                                {
                                    foreach (XmlNode xn3 in xn2.ChildNodes)
                                    {
                                        if (xn3.Attributes["value"].Value == time.ToString())
                                        {
                                            return xn3;
                                        }
                                    }
                                  
                                }
                            }
                           
                        }
                    }
                    
                   
                }
            }
            return null;
        } 
        static private  XmlNode FindAndInsertTime(XmlDocument xd, XmlNode root, int year, int month, int day, int time)
        {
            XmlElement temp,temp1,temp2,temp3;
            foreach(XmlNode xn0 in root.ChildNodes)
            {
                if (xn0.Attributes["value"].Value == year.ToString())
                {
                    foreach (XmlNode xn1 in xn0.ChildNodes)
                    {
                        if (xn1.Attributes["value"].Value == month.ToString())
                        {
                            foreach (XmlNode xn2 in xn1.ChildNodes)
                            {
                                if (xn2.Attributes["value"].Value == day.ToString())
                                {
                                    foreach (XmlNode xn3 in xn2.ChildNodes)
                                    {
                                        if (xn3.Attributes["value"].Value == time.ToString())
                                        {
                                            return xn3;
                                        }
                                    }
                                    temp=xd.CreateElement("Time");
                                    temp.SetAttribute("value",time.ToString());
                                    xn2.AppendChild(temp);
                                    return temp;
                                }
                            }
                            temp = xd.CreateElement("Day");
                            temp.SetAttribute("value", day.ToString());
                            xn1.AppendChild(temp);
                            temp1 = xd.CreateElement("Time");
                            temp1.SetAttribute("value", time.ToString());
                            temp.AppendChild(temp1);
                            return temp1;
                        }
                    }
                    temp = xd.CreateElement("Month");
                    temp.SetAttribute("value", month.ToString());
                    xn0.AppendChild(temp);
                    temp1 = xd.CreateElement("Day");
                    temp1.SetAttribute("value", day.ToString());
                    temp.AppendChild(temp1);
                    temp2 = xd.CreateElement("Time");
                    temp2.SetAttribute("value", time.ToString());
                    temp1.AppendChild(temp2);
                    return temp2;
                }
            }
            temp = xd.CreateElement("Year");
            temp.SetAttribute("value", year.ToString());
            root.AppendChild(temp);
            temp1 = xd.CreateElement("Month");
            temp1.SetAttribute("value", month.ToString());
            temp.AppendChild(temp1);
            temp2 = xd.CreateElement("Day");
            temp2.SetAttribute("value", day.ToString());
            temp1.AppendChild(temp2);
            temp3 = xd.CreateElement("Time");
            temp3.SetAttribute("value", time.ToString());
            temp2.AppendChild(temp3);
            return temp3;
        }

    }
#endregion
}
